using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Exceptions;
using RJB.S3Library;
using Browser.Helpers;
using Directory=RJB.S3.Entities.Folder;
using File=RJB.S3.Entities.File;

namespace Browser
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
        }
        private void butLogin_Click(object sender, EventArgs e)
        {
            PopulateFolders();
        }
        public void PopulateFolders()
        {
            FolderManager _dm = new FolderManager(Utils.RetrieveUserKey(), Utils.RetrieveSecretKey());
            List<Directory> _d = _dm.List();

            foreach (Directory directory in _d)
            {
                TreeNode _tn = new TreeNode(directory.Name);
                tvListing.Nodes.Add(_tn);
            }
        }
        public void PopulateFiles(string DirectoryName)
        {
            lvFiles.Items.Clear();
            FileManager _fm = new FileManager(Utils.RetrieveUserKey(), Utils.RetrieveSecretKey());
            List<File> _files = _fm.ListFiles(DirectoryName);

            foreach (File file in _files)
            {
                ListViewItem _lvi = new ListViewItem();
                _lvi.Text = file.Name;
                _lvi.Tag = DirectoryName;

                lvFiles.Items.Add(_lvi);
            }
        }
        private void tvListing_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Node.Text))
            {
                PopulateFiles(e.Node.Text);
            }
        }
        private void butDeleteFile_Click(object sender, EventArgs e)
        {
            if ( lvFiles.SelectedItems.Count > 0 )
            {
                ListViewItem _lvi = lvFiles.SelectedItems[0];
                string _dirName = _lvi.Tag.ToString();
                string _fileName = _lvi.Text;

                try
                {
                    FileManager _fm = new FileManager(Utils.RetrieveUserKey(), Utils.RetrieveSecretKey());
                    _fm.DeleteFile(_fileName, _dirName);
                }
                catch ( FileDeleteException fdex)
                {
                    MessageBox.Show(fdex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                PopulateFiles(_dirName);
            }
        }

        private void butAddFile_Click(object sender, EventArgs e)
        {
            //Only open if a now (i.e. directory is selected)
            if ( tvListing.SelectedNode != null )
            {
                frmAddFile _add = new frmAddFile();
                _add.FolderName = tvListing.SelectedNode.Text;
                _add.Show(this);
            }
        }
    }
}